Public Class SQLParseColumnDefinition
    Inherits SQLParser

    Private Function ExtractDataType(ByVal srcType As String, ByVal columnName As String) As String
        srcType = srcType.ToLower()
        Dim proj As BBXProject = Me.GetParserProject
        If proj.EnableDTStyleDateMapping Then
            If columnName.StartsWith("dt_") Then Return "System.DateTime"
        End If
        Select Case srcType
            Case "datetime", "timestamp"
                Return "System.DateTime"
            Case "uniqueidentifier"
                Return "System.Guid"
            Case "int"
                Return "System.Int32"
            Case "varchar", "nvarchar", "text", "ntext"
                Return "System.String"
            Case "bit"
                Return "System.Boolean"
            Case "float"
                Return "System.Double"
            Case "decimal", "smallmoney", "money", "numeric"
                Return "System.Decimal"
            Case "bigint"
                Return "System.Int64"
            Case "smallint"
                Return "System.Int16"
            Case "tinyint"
                Return "System.Byte"
            Case "real"
                Return "System.Single"
        End Select
        'MsgBox("Unsupported SQL data type: " & srcType & Environment.NewLine & "Using String instead...")
        Return "System.String"
    End Function

    Protected Overrides Function MyParserState() As SQLParserState
        Return SQLParserState.SQLParseColumnDefinition
    End Function

    Protected Friend Overrides Sub ParseCommands(ByVal Commands() As String, ByRef tables As TableList)
        Try
            If Commands.Length < 2 Then Return
            Dim proj As BBXProject = Me.GetParserProject
            If Commands(0).ToLower() = "constraint" Then
                If Commands(2).ToLower() = "primary" And Commands(3).ToLower() = "key" Then
                    Me.SetParserState(SQLParserState.SQLParsePrimaryKeyWithinTable)
                ElseIf Commands(2).ToLower() = "unique" Then
                    Me.SetParserState(SQLParserState.SQLParseUniqueKeyWithinTable)
                End If
                Return
            End If
            If Not (Commands(0).ToLower() = "on") Then
                Dim newColumn As New Column
                newColumn.ColumnName = Commands(0)
                newColumn.DataType = ExtractDataType(Commands(1), newColumn.ColumnName)
                If Commands.Length < 3 Then
                    newColumn.Length = 0
                Else
                    Dim nextIndex As Integer = 2
                    If Commands(1).ToLower() = "varchar" Or Commands(1).ToLower() = "nvarchar" Then
                        newColumn.Length = CInt(Val(Commands(2)))
                        newColumn.AddBoundsCheck = proj.EnableRuntimeBoundsCheck
                        nextIndex += 1
                End If
                If nextIndex < Commands.Length AndAlso Commands(nextIndex).ToLower.Contains("identity") Then
                    newColumn.Identity = True
                End If
                End If
                tables.ActiveTable.AddColumn(newColumn)
                Me.SetParserState(SQLParserState.SQLParseColumnDefinition)
            End If
        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try

    End Sub
End Class
